<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 14:40
 */
namespace App\Http\Controllers\Api\Notify;

use App\Http\Controllers\Controller;
use App\Repositories\CollectMoneyBillRepository;
use App\Repositories\PersonalAccountWithdrawRepository;
use App\Repositories\RepaymentAccountWithdrawRepository;
use App\Repositories\RepaymentPlanConsumeRepository;
use App\Repositories\RepaymentPlanFailHandlersRepository;
use App\Repositories\RepaymentPlanRepository;
use App\Repositories\ThreeApiErrorLogsRepository;
use App\Repositories\UserCreditCardRepository;
use App\Repositories\UserPassageRegisterRepository;
use App\Repositories\UserRepository;
use App\Services\CollectMoneyProfitService;
use App\Services\MessageService;
use App\Services\RepaymentProfitService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class QuickPayFNotifyController extends Controller
{
    protected $threeApiErrorLogsRepository;
    protected $userRepository;
    protected $userCreditCardRepository;
    protected $messageService;
    protected $collectMoneyProfitService;
    protected $apiKey;

    public function __construct(ThreeApiErrorLogsRepository $threeApiErrorLogsRepository,
                                UserRepository $userRepository,
                                UserCreditCardRepository $userCreditCardRepository,
                                MessageService $messageService,
                                CollectMoneyProfitService $collectMoneyProfitService)
    {
        $this->threeApiErrorLogsRepository = $threeApiErrorLogsRepository;
        $this->userRepository = $userRepository;
        $this->userCreditCardRepository = $userCreditCardRepository;
        $this->messageService = $messageService;
        $this->collectMoneyProfitService = $collectMoneyProfitService;
        $this->apiKey = config('pay.fourpay.api_key');
    }


    /**
     * 交易
     * subMechantOpenCardNotify:{"sign":"c49eb6085109a81c4ff26c9ef283369f","message":"\u6210\u529f:null","cardNo":"6225768649617527","dsorderid":"A911730952304802","code":"SUCCESS","mobile":"13926001684"}
     */
    public function trade(Request $request)
    {
        Log::info('quickMoneyNotifyF:' . json_encode($request->all()));
        $param = $request->all();
        $sign = $param['sign'];
        unset($param['sign']);
        if ($sign != getSign($param, $this->apiKey)) {
            Log::info('快捷支付F回调签名验证不正确');
            die('快捷支付F回调签名验证不正确');
        }
        $errorData = [
            'order_no' => $param['mch_no'],
            'error_code' => $param['order_status'],
            'error_msg' => json_encode($request->all()),
            'api_type' => 22
        ];
        $this->threeApiErrorLogsRepository->create($errorData);
        $this->collectMoneyProfitService->quickMoneyNotify($param);
    }
}